﻿package com.tongji.diy.view
{
	import com.tongji.diy.event.AddNewFurnitureEvent;
	import com.tongji.diy.event.AddRoomEvent;
	import com.tongji.diy.event.AddShapeEvent;
	import com.tongji.diy.event.FurnitureFinalStateEvent;
	import com.tongji.diy.event.FurnitureManualRotationEvent;
	import com.tongji.diy.event.FurnitureRotationEvent;
	import com.tongji.diy.event.LockFurnitureEvent;
	import com.tongji.diy.event.GetDoors;
	import com.tongji.diy.event.RoomRotationEvent;
	import com.tongji.diy.event.TestBoundBoxEvent;
	import com.tongji.diy.event.TestEnterClickEvent;
	import com.tongji.diy.event.TestShowLayOutEvent;
	import com.tongji.diy.event.UploadHoleEvent;
	import com.tongji.diy.event.UploadRoomBorderEvent;
	import com.tongji.diy.event.UploadRoomPositionEvent;
	import com.tongji.diy.event.UploadRoomRotationEvent;
	import com.tongji.diy.event.UploadRoomVertexEvent;
	import com.tongji.diy.event.furnitureOptimizationEvent.IterationCallbackEvent;
	import com.tongji.diy.event.furnitureOptimizationEvent.IterationPerFrameStartEvent;
	import com.tongji.diy.event.furnitureOptimizationEvent.IterationTestEvent;
	import com.tongji.diy.event.furnitureOptimizationEvent.OptimizationAddFurnitureEvent;
	import com.tongji.diy.event.ReturnDoorsInfoEvent;
	import com.tongji.diy.model.RoomDataVO;
	import com.tongji.diy.model.WallDoor;
	import com.tongji.diy.model.WallHole;
	import com.tongji.diy.model.furniturePlacementOptimization.OptimizationParameter;
	import com.tongji.diy.model.furniturePlacementOptimization.accessibleBoundingBoxes.FurnitureAcessibleBoundingBoxA;
	import com.tongji.diy.model.furniturePlacementOptimization.accessibleBoundingBoxes.FurnitureBasicBoundingBox;
	import com.tongji.diy.model.furniturePlacementOptimization.assembleComplement.FurnitureBase;
	import com.tongji.diy.model.furniturePlacementOptimization.complementStructs.OptimizationFurnitureAndRoomRelation;
	import com.tongji.diy.view.component3D.BaseRoomBorder;
	import com.tongji.diy.view.component3D.Door;
	import com.tongji.diy.view.component3D.Furniture;
	import com.tongji.diy.view.component3D.Room;
	import com.tongji.diy.view.component3D.RoomBorder;
	import com.tongji.diy.view.component3D.RoomBorderArc;
	import com.tongji.diy.view.component3D.RoomFloor;
	import com.tongji.diy.view.component3D.RoomVertex;
	import com.tongji.diy.view.component3D.ShapeToAdd;
	import com.tongji.diy.view.component3D.ShapeToAddArc;
	import com.tongji.diy.view.component3D.ShapeToAddLWall;
	import com.tongji.diy.view.component3D.ShapeToAddRect;
	import com.tongji.diy.view.filter.SelectFilter;
	
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	import mx.collections.ArrayCollection;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	import de.polygonal.ds.DListNode;
	
	import flare.basic.Scene3D;
	import flare.collisions.MouseCollision;
	import flare.core.Camera3D;
	import flare.core.Mesh3D;
	import flare.core.Pivot3D;
	import flare.core.Texture3D;
	import flare.events.MouseEvent3D;
	import flare.materials.Shader3D;
	import flare.materials.filters.ColorFilter;
	import flare.materials.filters.TextureFilter;
	import flare.primitives.Cube;
	import flare.primitives.Plane;
	import flare.system.Input3D;
	import flare.utils.Vector3DUtils;
	
	public class MainScene extends UIComponent
	{
		private var scene:Scene3D;
		//		private var dragingRoom:Room = null;
		public var horizontalPlane:Plane;
		private var currentRoom:Room;
		private var currentFurniture:Furniture;
		private var furnitureFilterEnable:Boolean = false;
		private var currentFurnitureLock:int = -1;
		
		//xu
		private var mouseFloorCollPos:Vector3D = null;
		private var floorMouseOver:RoomFloor = null;
		private var camera1:Camera3D = new Camera3D();
		private var camera2:Camera3D = new Camera3D();
		
		//---Sun
		private var shapeToAdd:ShapeToAdd = null;
		private var mouseBorderCollPos:Vector3D = null;
		private var borderMouseOver:RoomBorder = null;
		private var dragingObj:Pivot3D = null;
		private var doorToAdd:Door = null;
		
		private var wallPaperWindow:EditTitleWindow = null;
		private var originalMask:Shape;
		private var bWallPaperMask:Boolean = false;
		
		private var mouse:MouseCollision = new MouseCollision();
		//test
		private var tempwidth:Number=0;
		private var templength:Number=0;
		private var cube:Cube;
		//优化使用
		private var furnitureRoomRelationList:Vector.<OptimizationFurnitureAndRoomRelation> = new Vector.<OptimizationFurnitureAndRoomRelation>;
		private var iterationSign:Boolean = false;
		
		
		public function MainScene()
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			scene = new Scene3D(this);
			scene.camera = camera1;
		}
		
		private function init(e:Event):void 
		{
			resizeEvent();
			stage.addEventListener(Event.RESIZE, resizeEvent);
			scene.addEventListener( Scene3D.RENDER_EVENT, renderEvent );
			scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent );
			
			scene.addEventListener(UploadRoomVertexEvent.UPLOAD_ROOM_VERTEX,dispatch);
			scene.addEventListener(UploadRoomBorderEvent.UPLOAD_ROOM_BORDER,dispatch);
			
			//相机
			camera1.setPosition( 0, 800, -1200 );
			camera1.lookAt(0,0,0);
			
			//白色地平面
			var planeMaterial:Shader3D = new Shader3D( "planeMaterial" );
			planeMaterial.filters.push( new ColorFilter(0xffffff));
			planeMaterial.build();
			horizontalPlane = new Plane("mapPlane",10000,10000,1,planeMaterial);
			horizontalPlane.setRotation(90,0,0);
			horizontalPlane.y = -1;
			scene.addChild(horizontalPlane);
		}
		
		//test
		public function testBoundBox(e:TestBoundBoxEvent):void
		{
			if(tempwidth!=e.width&&templength!=e.length)
			{
				templength = e.length;
				tempwidth = e.width;
				cube = new Cube("boundBox",e.length,30,e.width,1,new Shader3D( "", [new ColorFilter(0xffffff)]));
				cube.x = e.location.x;
				cube.z = e.location.y;
				cube.setRotation(0,e.angle,0);
				scene.addChild(cube);
			}
			else
			{
				cube.x = e.location.x;
				cube.z = e.location.y;
				cube.setRotation(0,e.angle,0);
			}
		}
		
		public function dispatch(e:Event):void
		{
			this.dispatchEvent(e);
		}
		
		public function buildFurniture(modSrc:String):void
		{
			currentFurniture = new Furniture(modSrc);
			currentFurniture.load();
			currentFurniture.addEventListener(Event.COMPLETE,furnitureLoadComplete);
		}
		
		public function furnitureLoadComplete(e:Event):void
		{
			currentFurniture.setTransparent();
			horizontalPlane.addEventListener(MouseEvent3D.MOUSE_OVER,addFurniture);
		}
		
		private function addFurniture(e:MouseEvent3D):void
		{
			this.mouse.addCollisionWith(currentFurniture);		
			currentFurnitureLock = currentFurniture.furnitureIndex;
			horizontalPlane.removeEventListener(MouseEvent3D.MOUSE_OVER,addFurniture)
			currentFurniture.x = e.info.point.x;
			currentFurniture.z = e.info.point.z;
			currentFurniture.parent = scene;
			currentFurniture.startDrag( false ,new Vector3D(0,1,0));
			dragingObj = currentFurniture;
			var width:Number = (dragingObj as Furniture).width;
			var length:Number = (dragingObj as Furniture).length;
			var height:Number = (dragingObj as Furniture).height;
			dispatchEvent(new AddNewFurnitureEvent(AddNewFurnitureEvent.ADD_NEW_FURNITURE,
				currentFurniture.furnitureIndex,
				new Point(currentFurniture.x,currentFurniture.z),
				0,length,width,height,currentFurniture.furnitureClass));			
		}
		
		private function lockFurniture(e:MouseEvent3D):void
		{	
			if(currentFurnitureLock == ((e.target as Mesh3D).parent.parent as Furniture).furnitureIndex || currentFurnitureLock == -1)
			{
				currentFurnitureLock = ((e.target as Mesh3D).parent.parent as Furniture).furnitureIndex;
				currentFurniture = (e.target as Mesh3D).parent.parent as Furniture;
				currentFurniture.startDrag(false,new Vector3D(0,1,0));
				dragingObj = currentFurniture;
				dispatchEvent(new LockFurnitureEvent(LockFurnitureEvent.LOCK_FURNITURE, currentFurniture.furnitureIndex));
				(dragingObj as Furniture).setTransparent();
			}
		}
		
		public function buildRoom(roomData:RoomDataVO):void
		{
			currentRoom = new Room(roomData);
			horizontalPlane.addEventListener(MouseEvent3D.MOUSE_OVER,addRoom);
			this.mouse.addCollisionWith(currentRoom);
		}
		
		private function addRoom(e:MouseEvent3D):void
		{
			horizontalPlane.removeEventListener(MouseEvent3D.MOUSE_OVER,addRoom)
			currentRoom.x = e.info.point.x;
			currentRoom.z = e.info.point.z;
			currentRoom.parent = scene;
			currentRoom.startDrag( false ,new Vector3D(0,1,0));
			dragingObj = currentRoom;
			dispatchEvent(new AddRoomEvent(AddRoomEvent.ADD_ROOM));
		}
		
		public function renderEvent(e:Event):void 
		{
			//优化过程使用标志着优化过程开始
			if(iterationSign)
			{
				iterationSign = false;
				dispatch(new IterationCallbackEvent(IterationCallbackEvent.CALL_BACK));
			}
			
			
			if(dragingObj)
			{
				if (currentRoom&&dragingObj is Room)
				{
					updateCurrentRoomPosition();
					
				}
				if(dragingObj is RoomBorder)
				{
					var rb:RoomBorder = dragingObj as RoomBorder;
					rb.dragAct();
					rb.x = rb.z = 0;
				}
				if(dragingObj is RoomVertex)
				{
					var rv:RoomVertex = dragingObj as RoomVertex;
					rv.renderHandler();
				}
				
				if(dragingObj is Furniture)
				{
					//这批模型的mesh在这层
					if(this.mouse.test(Input3D.mouseX,Input3D.mouseY,true))
					{
						for(var k:int = 0; k < mouse.data.length; k++)
						{
							if(mouse.data[k].mesh.parent is Room)
							{
								currentRoom = mouse.data[k].mesh.parent as Room;
								break;
							}
						}
						var currentParentIsRoom:Boolean = false;
						if(currentFurniture.parent is Room)
							currentParentIsRoom = true;
						
						var width:Number = (dragingObj as Furniture).width;
						var length:Number = (dragingObj as Furniture).length;
						var height:Number = (dragingObj as Furniture).height;
						//rotation 以欧拉角存储
						var rotation:Number = (dragingObj as Furniture).rotation;
						dispatchEvent(new com.tongji.diy.event.FurnitureRotationEvent	(com.tongji.diy.event.FurnitureRotationEvent.FURNITURE_ROTATION, 
							currentFurniture.furnitureIndex,
							new Point(dragingObj.x,dragingObj.z),
							rotation,
							length,
							width,
							height,
							currentRoom.roomIndex,
							currentParentIsRoom
						));
					}
				}
			}
		}
		
		public function updateEvent(e:Event):void 
		{
			//测试使用 frank
			if(Input3D.keyHit(Input3D.ENTER))
				dispatch(new TestEnterClickEvent(TestEnterClickEvent.ENTER_CLICK));
			//每次必须更新弹窗位置的mask……
			maskUpdate();			
			
			if ( Input3D.keyDown( Input3D.UP ) )
				camera2.translateZ( 10 );
			if ( Input3D.keyDown( Input3D.DOWN ) )
				camera2.translateZ( -10 );
			
			// rotate the car over the y-up axis.
			if ( Input3D.keyDown( Input3D.LEFT ) )
				camera2.translateX( -10 );
			if ( Input3D.keyDown( Input3D.RIGHT ) )
				camera2.translateX( 10 );
			
			if (scene)
			{
				for (i = 0; i<scene.children.length; i++)
				{
					if (scene.children[i] is Room)
					{
						var room:Room = scene.children[i] as Room;
						for (var j:uint = 0; j<room.children.length; j++)
						{
							if (room.children[j] is RoomBorder)
							{
								var roomborder:RoomBorder = room.children[j] as RoomBorder;
								for (var k:int = 0; k<roomborder.doorVector.length; k++)
								{
									var camera2position:Vector3D = roomborder.globalToLocal(new Vector3D (camera2.x,camera2.y,camera2.z));
									if (roomborder.doorVector[k].x - camera2position.x < 90
										&& roomborder.doorVector[k].x - camera2position.x > -90
										&& roomborder.doorVector[k].z - camera2position.z < 90
										&& roomborder.doorVector[k].z - camera2position.z > -90)
//										roomborder.doorVector[k].visible = false;
									{
										//Door.open()
										roomborder.doorVector[k].open();
									}
									else
									{
										//Door.close()
										//roomborder.doorVector[k].close();
									}
								}
							}
						}
					}
				}
			}
			
			
			if ( Input3D.rightMouseDown )
			{
				if (scene.camera === camera1)
				{
					camera1.rotateY( Input3D.mouseXSpeed, false, Vector3DUtils.ZERO );
					camera1.rotateX( Input3D.mouseYSpeed, true, Vector3DUtils.ZERO );
					if (camera1.getPosition().y<0)
						camera1.rotateX( -Input3D.mouseYSpeed, true, Vector3DUtils.ZERO );
				}
				
				camera2.rotateY( Input3D.mouseXSpeed);
			}
			
			if ( Input3D.middleMouseDown)
			{
				camera1.translateX( -Input3D.mouseXSpeed * camera1.getPosition().length / 300 );
				camera1.translateY( Input3D.mouseYSpeed * camera1.getPosition().length / 300 );
			}
			
			if ( Input3D.delta != 0 ) camera1.translateZ( Input3D.delta*30);
			
			if (Input3D.mouseUp)
			{
				if(dragingObj)
				{
					if((dragingObj is Furniture && !furnitureFilterEnable) || !(dragingObj is Furniture))
						dragingObj.stopDrag();
					if(dragingObj is RoomBorder)
					{
						dragingObj.x = dragingObj.z = 0;
					}
					if(dragingObj is Room)
					{
						updateCurrentRoomRotation(true);
						updateCurrentRoomPosition(true);
					}
					if(dragingObj is Furniture && !furnitureFilterEnable)
					{
						currentFurnitureLock = -1;
						currentRoom.addChild(dragingObj,true);
						
						//以下一段代码为在优化中使用的家具房间关系列表，如果新的关系没在列表里则添加，否则不添加
						var addNewRelation:Boolean = true;
						for(var r:int = 0; r < furnitureRoomRelationList.length; r++)
						{
							if(furnitureRoomRelationList[r].furniture == dragingObj && furnitureRoomRelationList[r].room == currentRoom)
							{
								addNewRelation = false;
								break;
							}
						}
						if(addNewRelation)
						{
							var newFurnitureRoomRelation:OptimizationFurnitureAndRoomRelation = new OptimizationFurnitureAndRoomRelation(dragingObj as Furniture, currentRoom);
							furnitureRoomRelationList.push(newFurnitureRoomRelation);
						}

						var width:Number = (dragingObj as Furniture).width;
						var length:Number = (dragingObj as Furniture).length;
						var height:Number = (dragingObj as Furniture).height;
						var rotation:Number = (dragingObj as Furniture).rotation;
						(dragingObj as Furniture).setSolid();
						(dragingObj as Furniture).meshChildAddListener(MouseEvent3D.MOUSE_DOWN, lockFurniture);
						dispatchEvent(new FurnitureFinalStateEvent(FurnitureFinalStateEvent.FINAL_POSITION, new Vector3D(dragingObj.x,dragingObj.z,dragingObj.y),rotation, currentRoom.roomIndex));
						//以下优化使用
						var furnitureClass:int = (dragingObj as Furniture).furnitureClass;//furnitureClassSelect(dragingObj.name);
						dispatchEvent(new OptimizationAddFurnitureEvent(OptimizationAddFurnitureEvent.OPTIMIZATION_ADD_FURNITURE, 
							new Vector3D(dragingObj.x,dragingObj.z,dragingObj.y),
							width,length,height, rotation/180*Math.PI,
							(dragingObj.parent as Room).roomIndex, 
							(dragingObj as Furniture).furnitureIndex,
							furnitureClass)); 
						
					}
					if((dragingObj is Furniture && !furnitureFilterEnable) || !(dragingObj is Furniture))
						dragingObj = null;
				}
				
			}
			//MouseCollision
			if(this.mouse.test(Input3D.mouseX,Input3D.mouseY)&& scene.camera === camera1)
			{
				var mesh:Mesh3D = mouse.data[0].mesh;
				
				//RoomVertex
				if(mesh is RoomVertex && !(mesh as RoomVertex).lock)
				{
					//
					var vertex:RoomVertex = mesh as RoomVertex;
					var pos:Vector3D = mouse.data[0].point;
					
					//拖动
					if(Input3D.mouseDown&&!dragingObj)
					{
						dragingObj = vertex;
						dragingObj.startDrag(false,new Vector3D(0,1,0));
					}
					
					borderMouseOver.unSelect();
				}
				
				//RoomBorder
				else if(mesh is RoomBorder)
				{
					//选中效果
					if(borderMouseOver != mesh)
					{
						if(borderMouseOver)
						{
							borderMouseOver.unSelect();
						}
						this.borderMouseOver = mesh as RoomBorder;
						this.borderMouseOver.selectEffect();
					}
					else
					{
						if(!borderMouseOver.selected)
							borderMouseOver.selectEffect();
					}
					
					//鼠标所指的点
					this.mouseBorderCollPos = mouse.data[0].point;
					
					//双击弹窗
//					if(Input3D.mouseDobleClick)
					if(Input3D.keyDown(Input3D.NUMBER_0)&&!this.bWallPaperMask)
					{
//						trace("popup")
						this.bWallPaperMask = true;
						wallPaperWindow = EditTitleWindow(PopUpManager.createPopUp(this, EditTitleWindow , false));
						wallPaperWindow.x = 350;
						wallPaperWindow.y = 200;
						
						var wallPaper:ArrayCollection = new ArrayCollection();
						for (var i:int = 0; i < 8; i++)
						{
							var obj:Object = new Object();  
							obj.label = new String("墙纸" + i);
							obj.isSelected = new Boolean(true);
							wallPaper.addItem(obj);
						}
						wallPaperWindow.wallpaper.dataProvider = wallPaper;
						wallPaperWindow.commitButton.addEventListener(MouseEvent.CLICK,changeWallPaperCommit);
						wallPaperWindow.cancelButton.addEventListener(MouseEvent.CLICK,changeWallPaperCancel);
					}
					
					//RoomBorder的操作
					if (Input3D.mouseDown&&!dragingObj)
					{
						//房型变化
						if(shapeToAdd)
						{
							//多态
							if(this.mouseBorderCollPos !=  null && this.borderMouseOver != null)
								dispatch(new AddShapeEvent(AddShapeEvent.ADD_SHAPE, shapeToAdd, borderMouseOver, borderMouseOver.start.data.parentRoom));
							shapeToAdd.dispose();
							shapeToAdd = null;
						}
						//拖动
						else if(!dragingObj&&!borderMouseOver.lock)
						{
							borderMouseOver.startDragHandler();
							dragingObj = borderMouseOver;
							dragingObj.startDrag( false ,new Vector3D(0,1,0));
						}
						if(doorToAdd)
						{
							var door:Door = new Door(doorToAdd.wallDoor,this.mouseBorderCollPos,borderMouseOver);
							borderMouseOver.addDoor(door,this.mouse);
							doorToAdd.dispose();
							doorToAdd = null;
							dispatchEvent(new UploadHoleEvent(UploadHoleEvent.UPLOAD_HOLE,borderMouseOver,mouseBorderCollPos));
						}
					}
					
					//开墙洞事件   xu
					if (Input3D.middleMouseHit)
					{
						dispatchEvent(new UploadHoleEvent(UploadHoleEvent.UPLOAD_HOLE,borderMouseOver,mouseBorderCollPos));
					}
				}
				
				//RoomFloor
				else if(mesh is RoomFloor)
				{
					this.floorMouseOver = mesh as RoomFloor;
					this.mouseFloorCollPos = mouse.data[0].point;
					
					//拖动
					if (Input3D.mouseDown&&!dragingObj)
					{
						currentRoom = floorMouseOver.parent as Room;
						dragingObj = currentRoom;
						dragingObj.startDrag( false ,new Vector3D(0,1,0));
					}
					
					//旋转
					if (Input3D.keyDown(Input3D.LEFT))
					{
						dispatchEvent(new RoomRotationEvent(RoomRotationEvent.ROOM_ROTATION_COUNTERCLOCKWISE,currentRoom.roomIndex));
					}
					if (Input3D.keyDown(Input3D.RIGHT))
					{
						dispatchEvent(new RoomRotationEvent(RoomRotationEvent.ROOM_ROTATION_CLOCKWISE,currentRoom.roomIndex));
						
					}
					if(borderMouseOver)
						borderMouseOver.unSelect();
				}
				//furnitrure手动旋转
				else if(mesh is Mesh3D)
				{
					if (Input3D.keyDown(Input3D.LEFT))
					{
						dispatchEvent(new FurnitureManualRotationEvent(FurnitureManualRotationEvent.FURNITURE_ROTATION_CLOCKWISE,currentFurniture.furnitureIndex));
					}
					if (Input3D.keyDown(Input3D.RIGHT))
					{
						dispatchEvent(new FurnitureManualRotationEvent(FurnitureManualRotationEvent.FURNITURE_ROTATION_COUNTERCLOCKWISE,currentFurniture.furnitureIndex));
					}
				}
			}
			//没有选中任何mesh则去掉选中效果
			else
			{
				if(borderMouseOver)
					borderMouseOver.unSelect();
			}
			
			//处于修改模型状态
			if(shapeToAdd)
			{
				//多态
				if(this.mouseBorderCollPos !=  null && this.borderMouseOver != null)
					shapeToAdd.refresh(this.mouseBorderCollPos,this.borderMouseOver);
			}
			
			if(doorToAdd)
				if(this.mouseBorderCollPos !=  null && this.borderMouseOver != null)
					doorToAdd.refresh(this.mouseBorderCollPos,this.borderMouseOver);
			
			//切换漫游视角
			if ( Input3D.keyHit(Input3D.NUMBER_1))
			{
				scene.camera = camera1;
			}
			
			if ( Input3D.keyHit(Input3D.NUMBER_2)&&this.mouse.data[0].mesh is RoomFloor)
			{
				camera2.x = this.mouse.data[0].point.x;
				camera2.z = this.mouse.data[0].point.z;
				camera2.y = 150;
				scene.camera = camera2;
			}
			
		}
		
		public function changeIterationSign():void
		{
			iterationSign = true;
		}
		
		private function changeWallPaperCommit(e:MouseEvent):void
		{
			var mesh:Mesh3D = this.borderMouseOver as Mesh3D;
			var i:uint = wallPaperWindow.wallpaper.selectedIndex;
			var shader:Shader3D = mesh.surfaces[0].material as Shader3D;
			var texFilter:TextureFilter = shader.filters[0] as TextureFilter;
			//用addTextureFromFile会出现各种错误
			texFilter.texture = new Texture3D("./assets/image/SHO150110228MCUST001"+(++i)+"-1.jpg");
		}
		
		private function changeWallPaperCancel(e:MouseEvent):void
		{
			PopUpManager.removePopUp(wallPaperWindow);
			this.bWallPaperMask = false;
		}
		
		//更新mask
		private function maskUpdate():void
		{
			if (originalMask == null) 
			{
				originalMask = this.stage.getChildAt(0).mask as Shape;
			}
			this.stage.getChildAt(0).mask = originalMask;
			var mask:Shape = new Shape();
			mask.graphics.copyFrom(originalMask.graphics);
			if (bWallPaperMask)
			{
				mask.graphics.beginFill(0)
				mask.graphics.moveTo(wallPaperWindow.x, wallPaperWindow.y);
				mask.graphics.lineTo(wallPaperWindow.x + wallPaperWindow.width, wallPaperWindow.y);
				mask.graphics.lineTo(wallPaperWindow.x + wallPaperWindow.width, wallPaperWindow.y + wallPaperWindow.height);
				mask.graphics.lineTo(wallPaperWindow.x, wallPaperWindow.y + wallPaperWindow.height);
				mask.graphics.lineTo(wallPaperWindow.x, wallPaperWindow.y);
				mask.graphics.endFill();
			}
			this.stage.getChildAt(0).mask = mask;
		}
		
		private function resizeEvent(e:Event = null):void 
		{
			scene.setViewport(300, 0, stage.stageWidth - 300, stage.stageHeight, scene.antialias);
		}
		
		private function updateCurrentRoomPosition(register:Boolean = false):void
		{
			dispatchEvent(new UploadRoomPositionEvent(UploadRoomPositionEvent.UPLOAD_ROOM_POSITION,currentRoom.roomIndex,currentRoom.getPosition(),register))
		}
		
		
		private function updateCurrentRoomRotation(register:Boolean = false):void
		{
			dispatchEvent(new UploadRoomRotationEvent(UploadRoomRotationEvent.UPLOAD_ROOM_ROTATION,currentRoom.roomIndex,currentRoom.getRotation(),register))
		}
		
		public function setFurnitureLocationAndAngle(location:Vector3D, angle:Number):void//frank
		{
			currentFurniture.x = location.x;
			currentFurniture.z = location.y;
			currentFurniture.y = location.z;
			currentFurniture.setRotation(0,angle,0);
		}
		
		public function setCurrentRoomPosition(point:Point):void
		{
			currentRoom.x = point.x;
			currentRoom.z = point.y;
		}
		
		public function setCurrentRoomRotation(angle:Number):void//frank
		{
			currentRoom.setRotation(0,angle,0);
		}
		
		public function updateRoomBorder(updateBorderArray:Vector.<BaseRoomBorder>):void
		{
			for (var i:uint = 0; i<updateBorderArray.length; i++)
			{
				var border:BaseRoomBorder = updateBorderArray[i];
//				this.mouse.removeCollisionWith(border);
				border.refresh(this.mouse);
//				this.mouse.addCollisionWith(border);
			}
		}
		
		public function updateRoomFloor(updateFloor:RoomFloor):void
		{
			updateFloor.refresh();
		}
		
		public function updateRoomVertex(updateVertexArray:Vector.<RoomVertex>):void
		{
			for (var i:uint = 0; i<updateVertexArray.length; i++)
				(updateVertexArray[i] as RoomVertex).refresh();
		}
		
		public function showNewShape(newPointArray:Vector.<DListNode>,roomBorder:RoomBorder,shapeType:uint):void
		{
			//Rect4个点，LWall3个点
			for(var i:uint = 0;i<newPointArray.length;i++)
			{
				var vertex:RoomVertex = new RoomVertex(newPointArray[i],this.borderMouseOver.material);
				roomBorder.parent.addChild(vertex);
				this.mouse.addCollisionWith(vertex);
				
				var border:RoomBorder = new RoomBorder(newPointArray[i].prev,newPointArray[i],this.borderMouseOver.surfaces[0].material);
				roomBorder.parent.addChild(border);
				this.mouse.addCollisionWith(border);
			}
			
			//
			if(!shapeType)
			{
				//ShapeToAddRect
				border = new RoomBorder(newPointArray[newPointArray.length-1],newPointArray[newPointArray.length-1].next,this.borderMouseOver.surfaces[0].material);
				roomBorder.parent.addChild(border);
				this.mouse.addCollisionWith(border);
			}
			else
			{
				//ShapeToAddLWall
				//删点
				this.mouse.removeCollisionWith(roomBorder.end.data.vertex);
				roomBorder.parent.removeChild(roomBorder.end.data.vertex);
				roomBorder.end.data.vertex.dispose();
				//删墙
				this.mouse.removeCollisionWith(roomBorder.end.data.nextBorder);
				roomBorder.parent.removeChild(roomBorder.end.data.nextBorder);
				roomBorder.end.data.nextBorder.dispose();
				//画墙
				border = new RoomBorder(newPointArray[newPointArray.length-1],newPointArray[newPointArray.length-1].next,this.borderMouseOver.surfaces[0].material);
				roomBorder.parent.addChild(border);
				this.mouse.addCollisionWith(border);
			}
			
			//删墙
			roomBorder.parent.removeChild(roomBorder);
			this.mouse.removeCollisionWith(roomBorder);
			roomBorder.dispose();
			roomBorder = null;
		}
		
		public function showNewShapeArc(arcPointArray:Vector.<DListNode>,roomBorder:RoomBorder):void
		{
			//左点
			var vertex:RoomVertex = new RoomVertex(arcPointArray[0],this.borderMouseOver.material);
			roomBorder.parent.addChild(vertex);
			this.mouse.addCollisionWith(vertex);
			
			var border:BaseRoomBorder = new RoomBorder(arcPointArray[0].prev,arcPointArray[0],this.borderMouseOver.surfaces[0].material);
			roomBorder.parent.addChild(border);
			this.mouse.addCollisionWith(border);
			
			//控制点
			vertex = new RoomVertex(arcPointArray[1],this.borderMouseOver.material);
			roomBorder.parent.addChild(vertex);
			this.mouse.addCollisionWith(vertex);
			
			//右点
			vertex = new RoomVertex(arcPointArray[2],this.borderMouseOver.material);
			roomBorder.parent.addChild(vertex);
			this.mouse.addCollisionWith(vertex);
			
			border = new RoomBorder(arcPointArray[2],arcPointArray[2].next,this.borderMouseOver.surfaces[0].material);
			roomBorder.parent.addChild(border);
			this.mouse.addCollisionWith(border);
			
			//弧形墙
			border = new RoomBorderArc(arcPointArray[0],arcPointArray[2],arcPointArray[1],this.borderMouseOver.surfaces[0].material);
			roomBorder.parent.addChild(border);
			this.mouse.addCollisionWith(border);
			
			//控制点
			
			//删墙
			roomBorder.parent.removeChild(roomBorder);
			this.mouse.removeCollisionWith(roomBorder);
			roomBorder.dispose();
			roomBorder = null;
		}

		public function addHole(roomBorder:RoomBorder,position:Point):void
		{
			roomBorder.addHole(new WallHole(position,90,210),this.mouse)
			//trace (position);
		}
		
		public function addDoor(wallDoor:WallDoor):void
		{
			if(this.borderMouseOver!=null&&this.mouseBorderCollPos!=null&&shapeToAdd==null&&doorToAdd==null)
			{
				doorToAdd = new Door(wallDoor,this.mouseBorderCollPos,this.borderMouseOver);
				scene.addChild(doorToAdd);
			}
		}
		
		public function shapeToAddRect():void
		{
			if(this.borderMouseOver!=null&&this.mouseBorderCollPos!=null&&shapeToAdd==null&&doorToAdd==null)
			{
				var roomShapePoints:Vector.<Point> = new Vector.<Point>;
				
				roomShapePoints.push(new Point(-100,0));
				roomShapePoints.push(new Point(-100,200));
				roomShapePoints.push(new Point(100,200));
				roomShapePoints.push(new Point(100,0));
				
				shapeToAdd = new ShapeToAddRect(roomShapePoints,this.mouseBorderCollPos,this.borderMouseOver);
				scene.addChild(shapeToAdd);
			}
		}
		
		public function shapeToAddLWall():void
		{
			if(this.borderMouseOver!=null&&this.mouseBorderCollPos!=null&&shapeToAdd==null&&doorToAdd==null)
			{
				var roomShapePoints:Vector.<Point> = new Vector.<Point>;
				var endPos:Point = new Point(borderMouseOver.end.data.globalX,borderMouseOver.end.data.globalY);
				var dis:Number = Math.sqrt((endPos.x-mouseBorderCollPos.x)*(endPos.x-mouseBorderCollPos.x)+(endPos.y-mouseBorderCollPos.z)*(endPos.y-mouseBorderCollPos.z));
				
				roomShapePoints.push(new Point(0,0));
				roomShapePoints.push(new Point(0,200));
				roomShapePoints.push(new Point(dis,200));
				roomShapePoints.push(new Point(dis,0));
				
				shapeToAdd = new ShapeToAddLWall(roomShapePoints,this.mouseBorderCollPos,this.borderMouseOver);
				scene.addChild(shapeToAdd);
			}
		}
		
		public function shapeToAddArc():void
		{
			if(this.borderMouseOver!=null&&this.mouseBorderCollPos!=null&&shapeToAdd==null&&doorToAdd==null)
			{
				var arcControlPoints:Vector.<Point> = new Vector.<Point>;
				
				arcControlPoints.push(new Point(-200,0));
				arcControlPoints.push(new Point(0,400));
				arcControlPoints.push(new Point(200,0));
				
				shapeToAdd = new ShapeToAddArc(arcControlPoints,this.mouseBorderCollPos,this.borderMouseOver);
				scene.addChild(shapeToAdd);
			}
		}
//		临时弃用该函数，改为与其作用相同的同名函数
//		public function showCurrentLayout(optList:Vector.<FurnitureBasicBoundingBox>):void
//		{
//			for(var i:int = 0; i < optList.length; i++)
//			{
//				var sign:Boolean = true;
//				for(var j:int = 0; j < furnitureRoomRelationList.length && sign; j++)
//				{
//					if(optList[i].parentRoomIndex == furnitureRoomRelationList[j].room.roomIndex
//						&& optList[i].pairFurnitureIndex == furnitureRoomRelationList[j].furniture.furnitureIndex)
//					{
//						furnitureRoomRelationList[j].furniture.x = optList[i].location.x;
//						furnitureRoomRelationList[j].furniture.z = optList[i].location.y;
//						furnitureRoomRelationList[j].furniture.setRotation(0, optList[i].angle/Math.PI*180,0);
//						sign = false;
//					}
//				}
//			}
//		}
		//该函数将后台计算出的家具摆放呈现出来
		public function showCurrentLayout(e:IterationTestEvent):void
		{
			for(var j:int = 0; j < e.nearbylist.length; ++j)
			{
				for(var i:int = 0; i < furnitureRoomRelationList.length; ++i)
				{
					if(e.nearbylist[j].furnitureIndex == furnitureRoomRelationList[i].furniture.furnitureIndex)
					{
						var fur:Furniture = furnitureRoomRelationList[i].furniture;
						//fur.x = e.recorder[j].x;
						//fur.z = e.recorder[j].y;
						fur.x = e.nearbylist[j].x;
						fur.z = e.nearbylist[j].y;
						fur.y = e.nearbylist[j].z;
						//fur.setRotation(0, e.recorder[j].z/Math.PI*180,0);
						fur.setRotation(0, e.nearbylist[j].angle/Math.PI*180,0);
					}
				}
			}
			
			for(j = 0; e.facalList && j < e.facalList.length; ++j)
			{
				for(i = 0; i < furnitureRoomRelationList.length; ++i)
				{
					if(e.facalList[j].furnitureIndex == furnitureRoomRelationList[i].furniture.furnitureIndex)
					{
						fur = furnitureRoomRelationList[i].furniture;
						fur.x = e.facalList[j].x;
						fur.z = e.facalList[j].y;
						fur.y = e.facalList[j].z;
						fur.setRotation(0, e.facalList[j].angle/Math.PI*180,0);
					}
				}
			}
			
			for(j = 0; e.seatList && j < e.seatList.length; ++j)
			{
				for(i = 0; i < furnitureRoomRelationList.length; ++i)
				{
					if(e.seatList[j].furnitureIndex == furnitureRoomRelationList[i].furniture.furnitureIndex)
					{
						fur = furnitureRoomRelationList[i].furniture;
						fur.x = e.seatList[j].x;
						fur.z = e.seatList[j].y;
						fur.y = e.seatList[j].z;
						fur.setRotation(0, e.seatList[j].angle/Math.PI*180,0);
					}
				}
			}
			
			for(j = 0; e.beddingList && j < e.beddingList.length; ++j)
			{
				for(i = 0; i < furnitureRoomRelationList.length; ++i)
				{
					if(e.beddingList[j].furnitureIndex == furnitureRoomRelationList[i].furniture.furnitureIndex)
					{
						fur = furnitureRoomRelationList[i].furniture;
						fur.x = e.beddingList[j].x;
						fur.z = e.beddingList[j].y;
						fur.y = e.beddingList[j].z;
						fur.setRotation(0, e.beddingList[j].angle/Math.PI*180,0);
					}
				}
			}
			iterationSign = true;
			
		}
		
		public function furnitureOverlap(overlap:Boolean):void
		{
			if(overlap && !furnitureFilterEnable)
			{
				furnitureFilterEnable = true;
				(dragingObj as Furniture).setOverlap();
			}
			else if(!overlap && furnitureFilterEnable)
			{
				furnitureFilterEnable = false;
				(dragingObj as Furniture).removeOverlap();				
			}
		}
		
		public function testShowLayout(furV:Vector.<FurnitureBase>):void
		{
			for(var i:int = 0; i < furV.length; ++i)
				find(furV[i]);
				
			
			function find(furniturebase:FurnitureBase):void
			{
				for(var i:int = 0; i < furnitureRoomRelationList.length; ++i)
				{
					if(furniturebase.furnitureIndex == furnitureRoomRelationList[i].furniture.furnitureIndex)
					{
						var fur:Furniture = furnitureRoomRelationList[i].furniture;
						fur.x = furniturebase.x;
						fur.z = furniturebase.y;
						fur.y = furniturebase.z;
						fur.setRotation(0, furniturebase.angle/Math.PI*180,0);
						return;
					}
				}
			}
			
		}
		
		//zdp
		public function getdoors(e:GetDoors):void
		{
			//trace("3");
			var s_doors:Vector.<Door> = new Vector.<Door>();//传送信息到model，其中的width变量用于存放所属房间index。zdp
			var s_rooms:Vector.<int> = new Vector.<int>();
			if (scene)
			{
				for (var i:int = 0; i<scene.children.length; i++)
				{
					if (scene.children[i] is Room)
					{
						var room:Room = scene.children[i] as Room;
						for (var j:uint = 0; j<room.children.length; j++)
						{
							if (room.children[j] is RoomBorder)
							{
								var roomborder:RoomBorder = room.children[j] as RoomBorder;
								for (var k:int = 0; k<roomborder.doorVector.length; k++)
								{
									//trace(cameraroam.x,cameraroam.z);
									//roomborder.doorVector[k].refresh(roomborder.doorVector[k].pos,roomborder);
									//roomborder.doorVector[k]
									trace(roomborder.doorVector[k].x,roomborder.doorVector[k].z);
									s_doors.push(roomborder.doorVector[k]);
									s_rooms.push(i);
									
								}
							}
						}
					}
				}
			}
			//trace(s_doors.length,s_rooms.length);
			dispatch(new ReturnDoorsInfoEvent(ReturnDoorsInfoEvent.RETURNDOOSINFO,s_doors,s_rooms));
		}
	}
}